#!/bin/sh
# This script is called from the postinst of all libgl1-nvidia{,-legacy-*}-glx{,-ia32}, 
# xserver-xorg-video-nvidia* and libcuda1{,-ia32} packages.
set -e

. /usr/share/debconf/confmodule

package="nvidia-support"
templates="$(dpkg-query --control-path "$package" templates)"
if [ -n "$templates" ]; then
	db_x_loadtemplatefile "$templates" "$package"
fi


# Upstream version of the newly installed driver.
new_version="$1"

test -n "$new_version" || exit 0

warn_nvidia=""
warn_nouveau=""

# Is an nvidia module currently loaded?
if [ -e /proc/driver/nvidia/version ]; then

	running_version="$(grep NVRM /proc/driver/nvidia/version | awk '{ print $8 }')"

	if [ -z "$running_version" ]; then
		echo "ERROR: Cannot get version of currently loaded NVIDIA module." >&2
	elif [ "$running_version" != "$new_version" ]; then
		warn_nvidia="1"
	fi

fi

# Is the nouveau module currently loaded?
if [ -x /bin/lsmod ] && [ -n "$(lsmod 2>/dev/null | grep nouveau)" ]; then
	warn_nouveau="1"
fi

test -n "$warn_nvidia$warn_nouveau" || exit 0

# Allow preseeding to FALSE.
db_get nvidia-support/check-running-module-version
test "$RET" = "true" || exit 0

# Don't warn multiple times for the same version.
db_get nvidia-support/last-mismatching-module-version
test "$RET" != "$new_version" || exit 0

if [ "$warn_nvidia" ]; then
	db_subst nvidia-support/warn-mismatching-module-version running-version "$running_version"
	db_subst nvidia-support/warn-mismatching-module-version new-version "$new_version"
	db_fset nvidia-support/warn-mismatching-module-version seen false
	db_input high nvidia-support/warn-mismatching-module-version || true
fi
if [ "$warn_nouveau" ]; then
	db_fset nvidia-support/warn-nouveau-module-loaded seen false
	db_input high nvidia-support/warn-nouveau-module-loaded || true
fi
db_go || true
db_set nvidia-support/last-mismatching-module-version "$new_version"


exit 0
